package com.example.server.conf;

import com.example.server.security.AnnotationSessionBasedAuthorizeInterceptor;
import com.example.server.security.DefaultSessionBasedAuthenticateInterceptor;
import com.example.server.security.DefaultSessionBasedAuthorizeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration // 交给spring 管理； 定位： 配置类
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${yeb.enable-authorized-annotation}")
    private boolean enableAuthorize;
    @Autowired
    private DefaultSessionBasedAuthenticateInterceptor defaultSessionBasedAuthenticateInterceptor;

    @Autowired
    private DefaultSessionBasedAuthorizeInterceptor defaultSessionBasedAuthorizeInterceptor;

    @Autowired
    private AnnotationSessionBasedAuthorizeInterceptor annotationSessionBasedAuthorizeInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) { // 添加拦截器

        registry.addInterceptor(defaultSessionBasedAuthenticateInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/asserts/**")
                .excludePathPatterns("/swagger-resources");

        registry.addInterceptor(enableAuthorize ? annotationSessionBasedAuthorizeInterceptor : defaultSessionBasedAuthorizeInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/asserts/**")
                .excludePathPatterns("/swagger-resources");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 添加静态资源

        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}